home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2402 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.9 KB

  1. Path: noc.tor.hookup.net!the-fix.sos.on.ca
  2. From: <verneb@the-fix.sos.on.ca>
  3. Date: Sat,20 Jan 96 14:37:10 -0500
  4. Newsgroups: comp.lang.c
  5. Subject: Re: Help: floating point/use of floor
  6. Message-ID: <tcpnntpd.16.1.20.14.37.10.2781597121.351170@the-fix.sos.on.ca>
  7. References: <alun.champion.96jan19120052@g7240065.bridge.bst.bls.com>
  8. Organization: the FIX - Kitchener, Ontario CANADA
  9. NNTP-Posting-Host: the-fix.sos.on.ca
  10. X-MajorTCP-Version: MajorTCP/IP [1.81-2]
  11.  
  12. To: INT:Alun.Champion@bridge.
  13.  
  14. Int:alun.champion@bridge.,
  15.  
  16. I>In article <4dogri$fta@gate.service.britgas.co.uk>
  17. I>andy.crutchlow@service.britgas.co.uk (Andy Crutchlow) writes:
  18. I>
  19. I>: Can anyone help.
  20. I>
  21. I>: Make the following display 75.99
  22. I>: main()
  23. I>: {
  24. I>: int i;
  25. I>: float f;
  26. I>: i = 7599;
  27. I>: f = i / 100.00;
  28. I>: printf("%f\n",f);/* change to: printf("%.2f\n",f);
  29. I>: return 0;  /* don't forget the return here */
  30.     }
  31. I>
  32.     
  33.     problem with floats (and doubles) is that some numbers are not
  34. I>directly representable.
  35. I>What may not be directly representable in floats may be representable in
  36. I>doubles.
  37. I>I can make it print 75.99 but I can't guarentee yours will do the same.
  38. I>
  39. I>..h>
  40. I>
  41. I>int
  42. I>main(void)
  43. I>{
  44. I>float f = 75.99;
  45. I>double d = 75.99;
  46. I>printf("%f\n", f);      /* prints 75.989998 */
  47. I>printf("%.2f\n", f);    /* prints 75.99 */
  48. I>printf("%g\n", f);      /* prints 75.99 */
  49. I>printf("%e\n", f);      /* prints 7.599000e+01 */
  50. I>
  51. I>printf("%f\n", d);      /* prints 75.990000 */
  52. I>printf("%g\n", d);      /* prints 75.99 */
  53. I>printf("%e\n", d);      /* prints 7.599 */
  54. I>}
  55. I>
  56. I>: I believe the correct answer involves using function floor but I can't
  57. I>: remember how.
  58. I>
  59. I>floor() will not help - it returns the largest integer not greater than
  60. I>its argument.
  61. I>
  62. I>Regards
  63. I>
  64. I>-A.
  65. I>-- 
  66. I>| A.Champion                |
  67. I>
  68. I>                                                                       
  69.  
  70. Verne H. Bohlender
  71. Box 416, Chesley, Ontario.Ca. N0G 1L0
  72. verneb@The-Fix.sos.on.ca
  73. ___
  74. * UniQWK #2158*
  75.  
  76.